package com.macrohard.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macrohard.admin.domain.ScoreExcelModel;
import com.macrohard.admin.service.ScoreAnalyzeService;
import com.macrohard.admin.utils.poi.ExcelUtil;
import com.macrohard.common.base.response.R;
import com.macrohard.mapper.domain.Grade;
import com.macrohard.mapper.domain.ScoreAnalyze;
import com.macrohard.mapper.domain.Teacher;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/score")
@Validated
@RequiredArgsConstructor
@Api(tags = "成绩分析接口")
public class ScoreController {
	private final ScoreAnalyzeService scoreAnalyzeService;

	@GetMapping
	@ApiOperation("分页获取所有成绩分析")
	public R get(@NotNull Integer current, @NotNull Integer pageSize) {
		Page<ScoreAnalyze> page = new Page<>(current, pageSize);
		LambdaQueryWrapper<ScoreAnalyze> queryWrapper = new LambdaQueryWrapper<>();
		page = scoreAnalyzeService.page(page, queryWrapper);
		return R.ok("ok", page);
	}

	@ApiOperation("根据学生姓名或学号获取模块成绩分析")
	@GetMapping("c")
	public R get(@ApiParam("学生姓名") String stuName, @ApiParam("学生学号") String stuId) {
		if (Objects.isNull(stuName) && Objects.isNull(stuId)) {
			return R.ok("ok", Collections.emptyList());
		}
		LambdaQueryWrapper<ScoreAnalyze> queryWrapper = new LambdaQueryWrapper<>();
		if (Objects.nonNull(stuId)) {
			queryWrapper = queryWrapper.eq(ScoreAnalyze::getsStuid, stuId);
			return R.ok("ok", Collections.singletonList(scoreAnalyzeService.getOne(queryWrapper)));
		}
		queryWrapper = queryWrapper.like(ScoreAnalyze::getsStuname, stuName);
		return R.ok("ok", scoreAnalyzeService.list(queryWrapper));
	}

	@ApiOperation("根据id获取成绩分析")
	@GetMapping("{id}")
	public R get(@PathVariable Integer id) {
		return R.ok("ok", scoreAnalyzeService.getById(id));
	}

	@ApiOperation("添加成绩分析")
	@PostMapping
	public R add(@RequestBody ScoreAnalyze ScoreAnalyze) {
		return R.wrapEffectResult(scoreAnalyzeService.save(ScoreAnalyze));
	}

	@PutMapping
	@ApiOperation("修改成绩分析")
	public R update(@RequestBody ScoreAnalyze ScoreAnalyze) {
		return R.wrapEffectResult(scoreAnalyzeService.updateById(ScoreAnalyze));
	}

	@ApiOperation("根据id删除成绩分析")
	@DeleteMapping("{id}")
	public R delete(@PathVariable Integer id) {
		return R.wrapEffectResult(scoreAnalyzeService.removeById(id));
	}

	@ApiOperation("批量删除成绩分析")
	@DeleteMapping
	public R delete(@RequestBody List<Integer> ids) {
		return R.wrapEffectResult(scoreAnalyzeService.removeByIds(ids));
	}

	@GetMapping("excel")
	public void download(HttpServletResponse response) {
		List<ScoreExcelModel> list = scoreAnalyzeService.list().stream().map(ScoreExcelModel::new).collect(Collectors.toList());
		ExcelUtil.exportExcel(list, "成绩分析", ScoreExcelModel.class, response);
	}
}
